package com.mathdroid.gui;

import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;

import com.mathdroid.exceptionhandling.ExceptionHandler;

/**
 * This image loader provides convenient methods to create ImageIcons. Pathes
 * are easy to modify. The ClassLoader is used to load the files.
 */
public final class ImageLoader {
	
	/**
	 * The path to all image files.
	 */
	private static final String RESOURCES = "/com/mathdroid/gui/images/";
	
	/**
	 * The folder of images for items. This is relative to RESOURCES.
	 */
	private static final String ITEM_PATH = "items/";
	
	/**
	 * The folder of images for advertising. This is relative to RESOURCES.
	 */
	private static final String AD_PATH = "ads/";
	
	/**
	 * The folder of images for abilities. This is relative to RESOURCES.
	 */
	private static final String ABILITY_PATH = "abilities/";
	
	/**
	 * The folder of images for talents. This is relative to RESOURCES.
	 */
	private static final String TALENTS_PATH = "talents/";
	
	/**
	 * The folder of images for group buffs. This is relative to RESOURCES.
	 */
	private static final String GROUP_BUFFS_PATH = "groupbuffs/";
	
	/**
	 * The folder of images for filters. This is relative to RESOURCES.
	 */
	private static final String FILTER_PATH = "filter/";
	
	/**
	 * The suffix of disabled filter images.
	 */
	private static final String FILTER_DISABLED_SUFFIX = "_disabled";
	
	/**
	 * The edge length of small item icons (30px). Normal icons have 50px.
	 */
	private static final int SMALL_IMAGE_SCALE = 30;

	/**
	 * Get an enabled filter image.
	 * @param name Name of the filter image.
	 * @return ImageIcon.
	 */
	public ImageIcon getEnabledFilterImage(final String name) {
		return this.getImage(FILTER_PATH + name);
	}
	
	/**
	 * Get a disabled filter image.
	 * @param name Name of the filter image.
	 * @return ImageIcon.
	 */
	public ImageIcon getDisabledFilterImage(final String name) {
		return this.getImage(FILTER_PATH + name + FILTER_DISABLED_SUFFIX);
	}
	
	/**
	 * Get an icon for an item. Note: Those images are JPEGS.
	 * @param name The icon name.
	 * @return ImageIcon.
	 */
	public ImageIcon getItemIcon(final String name) {
		ImageIcon i = null;
		
		try {
			i = new ImageIcon(this.getClass().getResource(
					RESOURCES 
					+ ITEM_PATH 
					+ name 
					+ ".jpg"));
		} catch (Exception e) {
			ExceptionHandler.report(
					"Item image not found: \"" + name + "\"", e);
		}
		
		return i;
	}
	
	/**
	 * Get an icon for an ability.
	 * @param name The icon name.
	 * @return ImageIcon.
	 */
	public ImageIcon getAbilityIcon(final String name) {
		return new ImageIcon(this.getClass().getResource(
				RESOURCES 
				+ ABILITY_PATH 
				+ name 
				+ ".jpg"));
	}
	
	/**
	 * Get an image for an talent. This returns an Image, not ImageIcon.
	 * @param name The image name.
	 * @return Image.
	 */
	public Image getTalentIcon(final String name) {
		return new ImageIcon(this.getClass().getResource(
				RESOURCES 
				+ TALENTS_PATH 
				+ name 
				+ ".jpg")).getImage();
	}
	
	/**
	 * Get an icon for an group buff.
	 * @param name The icon name.
	 * @return ImageIcon.
	 */
	public ImageIcon getGroupBuffIcon(final String name) {
		return this.getImage(GROUP_BUFFS_PATH + name);
	}
	
	/**
	 * Get an image for an advertising.
	 * @param name The image name.
	 * @return ImageIcon.
	 */
	public ImageIcon getAdImage(final String name) {
		return this.getImage(AD_PATH + name);
	}
	
	/**
	 * Get an empty slot icon with 30x30 dimensions.
	 * @return ImageIcon.
	 */
	public ImageIcon getEmptyIcon30() {
		return this.getImage("empty30");
	}
	
	/**
	 * Get an empty slot icon with 50x50 dimensions.
	 * @return ImageIcon.
	 */
	public ImageIcon getEmptyIcon50() {
		return this.getImage("empty50");
	}
	
	/**
	 * Get an icon from the standard folder.
	 * @param name Name of the icon.
	 * @return ImageIcon.
	 */
	public ImageIcon getImage(final String name) {
		return new ImageIcon(
				this.getClass().getResource(RESOURCES + name + ".png"));
	}

	/**
	 * Get an <b>SMALL</b> (30x30) icon for an item. 
	 * Note: Those images are JPEGS.
	 * @param name The icon name.
	 * @return ImageIcon.
	 */
	public ImageIcon getSmallItemIcon(final String name) {
		return new ImageIcon(
				Toolkit.getDefaultToolkit().getImage(
						this.getClass().getResource(RESOURCES + ITEM_PATH 
													+ name + ".jpg")
				).getScaledInstance(
						SMALL_IMAGE_SCALE, 
						SMALL_IMAGE_SCALE, 
						Image.SCALE_SMOOTH));
	}
}
